import { setToken, getToken, removeToken } from '@/utils/auth'
import { login, getUserInfo, getUserDetailById } from '@/api'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userinfo: {}
  },
  mutations: {
    Set_Token(state, token) {
      state.token = token
      setToken(token)
    },
    Set_UserInfo(state, userinfo) {
      state.userinfo = userinfo
    },
    Remove_Token(state) {
      state.token = ''
      state.userinfo = {}
      removeToken()
      // 重置路由
      resetRouter()
    }

  },
  actions: {
    async fetchLogin(store, loginFrom) {
      const { data } = await login(loginFrom)
      store.commit('Set_Token', data)
      return data
    },
    async fetchUserInfo(store) {
      const res = await getUserInfo()
      const res1 = await getUserDetailById(res.data.userId)
      store.commit('Set_UserInfo', { ...res.data, ...res1.data })
      console.log(res.data)
      return res.data.roles
    }
  }
}
